<?php
declare (strict_types = 1);

namespace app\product\controller;

use app\auth\common\util\ApiUtil;
use app\product\BaseController;
use app\product\service\RegionAreaService;
use app\product\service\RegionCityService;
use app\product\service\RegionProvinceService;

class CommonController extends BaseController
{
    public function __construct(RegionProvinceService $regionProvinceService,
                                RegionCityService $regionCityService,
                                RegionAreaService $regionAreaService){
        $this->regionProvinceService = $regionProvinceService;
        $this->regionCityService = $regionCityService;
        $this->regionAreaService = $regionAreaService;
    }

    public function getProvince(){
        $list = $this->regionProvinceService->select();
        return ApiUtil::normal( $list );
    }

    public function getCity(){
        $code = input('province_code',0);
        if(empty($code)) return ApiUtil::normal(null, '省份代码不能为空', ApiUtil::CODE_FAIL);
        $list = $this->regionCityService->select(['province_code'=>$code]);
        return ApiUtil::normal($list);
    }

    public function getArea(){
        $code = input('city_code',0);
        if(empty($code)) return ApiUtil::normal(null, '城市代码不能为空', ApiUtil::CODE_FAIL);
        $list = $this->regionAreaService->select(['city_code'=>$code]);
        return ApiUtil::normal($list);
    }
}
